﻿Imports System.Text.RegularExpressions
Public Class HiddenForm1

    Dim countresults As Integer

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        Dim resultslist As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("h3")
        Dim resultitem As HtmlElement
        Dim urla As String
        Dim name As String
        Dim temp As String
        Dim desc1 As String = MainSearchForm.txtSearchString.Text
        Dim desc As String = MainSearchForm.txtSearchString.Text
        countresults = 0

        For Each resultitem In resultslist
            If countresults < 10 Then
                If resultitem.GetAttribute("classname") = "r" Then
                    If Not (resultitem.NextSibling Is Nothing) Then
                        desc1 = resultitem.NextSibling.InnerHtml

                        Dim RegexObj As New Regex("<span class=""st"">(.*)</span>", RegexOptions.IgnoreCase)
                        Dim MatchResults As Match = RegexObj.Match(desc1)
                        While MatchResults.Success
                            desc = MatchResults.Groups(1).Value
                            temp = Replace(desc, "<em>", "")
                            temp = Replace(temp, "<span class=""f"">", "")
                            temp = Replace(temp, "</span>", "")
                            temp = Replace(temp, "<br>", "")
                            temp = Replace(temp, "<b>", "")
                            temp = Replace(temp, "</b>", "")
                            desc = Replace(temp, "</em>", "")
                            MatchResults = MatchResults.NextMatch()
                        End While
                    End If
                    urla = resultitem.FirstChild.GetAttribute("href").ToString()
                    name = resultitem.FirstChild.InnerHtml
                    temp = Replace(name, "<b>", "")
                    temp = Replace(temp, "</b>", "")
                    temp = Replace(temp, "<em>", "")
                    name = Replace(temp, "</em>", "")
                    MainSearchForm.apotelesmata.addResult(name, urla, "google", desc)
                    countresults = countresults + 1
                    Try
                        MainSearchForm.ProgressBar1.Value = MainSearchForm.ProgressBar1.Value + 1
                    Catch
                    End Try
                End If
            Else
                Exit For
            End If
        Next

        resultslist = WebBrowser1.Document.GetElementsByTagName("td")
        For Each resultitem In resultslist
            If MainSearchForm.apotelesmata.googlecount < 99 Then
                If resultitem.GetAttribute("classname") = "b navend" Then
                    urla = resultitem.FirstChild.GetAttribute("href").ToString()
                    WebBrowser1.Navigate(urla)
                End If
            Else
                MainSearchForm.flag1 = True
                MainSearchForm.ReadyToView()
                Exit For
            End If
        Next
    End Sub
End Class
